//
//  JSCollectionViewHelper.m
//  JSBasicFramework
//
//  Created by Apple on 2018/8/2.
//  Copyright © 2018年 Apple. All rights reserved.
//

#import "JSCollectionViewHelper.h"
#import "JSUITool.h"

@interface JSCollectionViewHelper ()

@end

@implementation JSCollectionViewHelper
-(instancetype)initWithOwner:(id)owner Regiter:(RegisterWithIdentifer)registerBefore
               sectionNumber:(SectionNumber)sectionNumber
                   rowNumber:(RowNumber)rowNumber
               cellIdentifer:(CellIdentifer)cellIdentifer
             headerIdentifer:(HeaderIdentifer)headerIdentifer
             footerIdenrifer:(FooterIdentifer)footerIdentifer
                cellWithSize:(CellWithSize)cellWithSize
              headerWithSize:(HeaderWithSize)headerWithSize
              footerWithSize:(FooterWithSize)footerWithSize
                  cellConfig:(CellConfigData)cellConfig
                headerConfig:(HeaderConfigData)headerConfig
                footerConfig:(FooterConfigData)footerConfig
          didSelectIndexPath:(DidSelectIndexPath)didSelectIndexPath
               sectionInsets:(SectionInsets)sectionInsets
                 lineSpacing:(MinimumLineSpacingForSection)lineSpacing
            interItemSpacing:(MinimumInteritemSpacingForSection)itemSpacing{
    if (self = [super initWithOwner:owner Regiter:registerBefore sectionNumber:sectionNumber rowNumber:rowNumber cellIdentifer:cellIdentifer headerIdentifer:headerIdentifer footerIdenrifer:footerIdentifer cellWithSize:cellWithSize headerWithSize:headerWithSize footerWithSize:footerWithSize cellConfig:cellConfig headerConfig:headerConfig footerConfig:footerConfig didSelectIndexPath:didSelectIndexPath]) {
        self.sectionInsets = sectionInsets;
        self.lineSpacing = lineSpacing;
        self.itemSpacing = itemSpacing;
    }
    return self;
}

- (instancetype)initWithLayout:(UICollectionViewLayout *)layout Regiter:(RegisterWithIdentifer)registerBefore sectionNumber:(SectionNumber)sectionNumber rowNumber:(RowNumber)rowNumber cellIdentifer:(CellIdentifer)cellIdentifer headerIdentifer:(HeaderIdentifer)headerIdentifer footerIdenrifer:(FooterIdentifer)footerIdentifer cellWithSize:(CellWithSize)cellWithSize headerWithSize:(HeaderWithSize)headerWithSize footerWithSize:(FooterWithSize)footerWithSize cellConfig:(CellConfigData)cellConfig headerConfig:(HeaderConfigData)headerConfig footerConfig:(FooterConfigData)footerConfig didSelectIndexPath:(DidSelectIndexPath)didSelectIndexPath sectionInsets:(SectionInsets)sectionInsets lineSpacing:(MinimumLineSpacingForSection)lineSpacing interItemSpacing:(MinimumInteritemSpacingForSection)itemSpacing{
    if (!layout) {
        UICollectionViewFlowLayout *layout1 = [[UICollectionViewFlowLayout alloc] init];
        layout1.scrollDirection = UICollectionViewScrollDirectionVertical;
        layout = layout1;
    }
    _collectionViewLayout = layout;
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
    collectionView.backgroundColor = [UIColor whiteColor];
    if (self = [super initWithOwner:collectionView Regiter:registerBefore sectionNumber:sectionNumber rowNumber:rowNumber cellIdentifer:cellIdentifer headerIdentifer:headerIdentifer footerIdenrifer:footerIdentifer cellWithSize:cellWithSize headerWithSize:headerWithSize footerWithSize:footerWithSize cellConfig:cellConfig headerConfig:headerConfig footerConfig:footerConfig didSelectIndexPath:didSelectIndexPath]) {
        self.sectionInsets = sectionInsets;
        self.lineSpacing = lineSpacing;
        self.itemSpacing = itemSpacing;
        _collectionView = collectionView;
    }
    return self;
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    if (self.sectionInsets) {
        return self.sectionInsets(collectionView, section);
    }
    CGFloat space = KREALVALUE(10);
    return UIEdgeInsetsMake(space, space,space,space);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    if (self.itemSpacing) {
        return self.itemSpacing(collectionView,section);
    }
    return KREALVALUE(10);
}

//动态设置每行的间距大小

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    if (self.lineSpacing) {
        return self.lineSpacing(collectionView,section);
    }
    return KREALVALUE(10);
}
#pragma mark - JHCollectionViewDelegateFlowLayout
- (UIColor *)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout backgroundColorForSection:(NSInteger)section{
    if (self.customSectionBgColor) {
        return self.customSectionBgColor(self, section);
    }
    return self.collectionView?self.collectionView.backgroundColor:[UIColor whiteColor];
}


@end
